package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.Role;
import com.example.result.Result;
import com.example.service.RoleService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 角色控制层
 * @date 2019-12-31
 */
@RestController
@RequestMapping("/api")
public class RoleController {

    @Autowired
    RoleService roleService;

    // =============================角色管理===================================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加角色信息
     */
    @RequestMapping(value = "/roles", method = RequestMethod.POST)
    public Result addRole(@RequestBody String json) {
        // 使用GSON将JSON数据转换为Role对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Role role = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Role.class);
        return roleService.addRole(role);
    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新角色信息
     */
    @RequestMapping(value = "/roles", method = RequestMethod.PUT)
    public Result updateRole(@RequestBody String json) {
        // 使用GSON将JSON数据转换为Role对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Role role = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Role.class);
        return roleService.updateRole(role);
    }

    /**
     * @param roleId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据编号删除角色信息
     */
    @RequestMapping(value = "/roles/{roleId}", method = RequestMethod.DELETE)
    public Result deleteRoleById(@PathVariable("roleId") String roleId) {
        return roleService.deleteRoleById(roleId);
    }

    /**
     * @return 以JSON形式返回所有角色信息
     * @describe 查询所有角色信息
     */
    @RequestMapping(value = "/roles", method = RequestMethod.GET)
    public Result queryAllRole() {
        return roleService.queryAllRole();
    }

    /**
     * @param roleId
     * @return 以JSON形式返回该角色信息
     * @describe 根据编号查询该角色信息
     */
    @RequestMapping(value = "/roles/{roleId}", method = RequestMethod.GET)
    public Result queryRoleById(@PathVariable("roleId") String roleId) {
        return roleService.queryRoleById(roleId);
    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加角色信息
     */
    @RequestMapping(value = "/roles/excels", method = RequestMethod.POST)
    public Result addRoleByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<Role> list = JSON.parseArray(parseObject.getString("json"), Role.class);

        System.out.println(list);

        return roleService.addRoleByExcel(list);

    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 角色信息模糊查询
     */
    @RequestMapping(value = "/roles/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return roleService.serachByInfo(info);

    }

    /**
     * @param role_Id
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据角色编号批量删除角色信息
     */
    @RequestMapping(value = "/roles/dels", method = RequestMethod.POST)
    public Result deleteRoleByIds(@RequestBody Map<String, String> roleIds) {

        return roleService.deleteRoleByIds(roleIds);
    }

}
